/**
 * Created by ptt on 2016/5/27/0027.
 */
define(["../../../../scripts/gis/gis/loadMap.js"], function (map) {
    var map = map.map;
    var statistical = [
        {
            index: 1,
            title: "规划成果",
            content: ["规划分布",],
            url: mapURL.szRoadGh
        },
        {
            index: 2,
            title: "建设项目",
            content: ["前期项目分布", "在建项目分布"],
            url: ""
        },
        {
            index: 3,
            title: "道路基础路网",
            content: ["道路网密", "道路网分", "桥梁分布"],
            url: mapURL.szRoadurl
        },
        {
            index: 4,
            title: "路况",
            content: ["路况分布", "各辖区统计图"],
            url: mapURL.szPciurl
        },
        {
            index: 5,
            title: "桥梁",
            content: ["等级分布", "各辖区统计图"],
            url: mapURL.szPciurl
        },
        {
            index: 6,
            title: "日常养护",
            content: ["养护区域分布", "巡查轨迹", "病害分布", "保养分布", "小修项目分布", "三危地图"],
            url: mapURL.szRoadbh
        },
        {
            index: 7,
            title: "大中修项",
            content: ["在建项目分布"],
            url: ""
        }
    ];

    function toggleLayer(mapUrl, title, content) {
    	if (!mapUrl) return;
        if(title=="道路基础路网")
        {
           if(szvector) {
               var qunames=new Array();
              var features= szvector.getSource().getFeatures();
               if (features.length > 0) {
                   for (var i = 0; i < features.length; i++) {
                       var quname=features[i].get("qucode");
                       if(qunames.indexOf(quname)<0) {
                           qunames.push(quname);
                           var centerpoint = ol.extent.getCenter(features[i].getGeometry().getExtent());
                           var popup = new ol.Overlay.Popup({insertFirst: false});
                           map.addOverlay(popup);
                           var srcurl = "http://localhost:8080/ssjc/taglib/roadbasicinfo!showByDistrict.action?fromParam=amount&type=101&district=" + quname + " frameborder='0' scrolling='no'";
                           popup.show(centerpoint, '<div><iframe src=' + srcurl + ' width="200" height="200"> <p>');
                       }
                   }

               }
               else {
                   map.removeOverlay(map.getOverlays());
               }
           }
        }
        else if(title=="规划成果")
        {

            var image = new ol.style.Circle({
                radius: 5,
                fill: null,
                stroke: new ol.style.Stroke({color: 'red', width: 1})
            });
            var styles = {
                'Point': new ol.style.Style({
                    image: image
                }),
                'LineString': new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color: 'green',
                        width: 1
                    })
                }),
                'MultiLineString': new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color: 'green',
                        width: 1
                    })
                }),
                'MultiPoint': new ol.style.Style({
                    image: image
                }),
                'MultiPolygon': new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color: 'yellow',
                        width: 1
                    }),
                    fill: new ol.style.Fill({
                        color: 'rgba(255, 255, 0, 0.1)'
                    })
                }),
                'Polygon': new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color: 'blue',
                        lineDash: [4],
                        width: 3
                    }),
                    fill: new ol.style.Fill({
                        color: 'rgba(0, 0, 255, 0.1)'
                    })
                })
            };
            var styleFunction = function(feature) {
            return styles[feature.getGeometry().getType()];
        };

            var vectorSource = new ol.source.Vector();
            fetch("http://localhost:8080/ssjc/rep/damagedisease!provideGeoJson.action?beginTime=2013-07-16&endTime=2013-07-17").
            then(function(response) {
                return response.json();
            }).then(function(json) {
                var format = new ol.format.GeoJSON();
                var features = format.readFeatures(json,{featureProjection: 'EPSG:3857'});
                vectorSource.addFeatures(features);
            });

            var vectorLayer = new ol.layer.Vector({
                source: vectorSource,
                style: styleFunction
            });
            map.addLayer(vectorLayer);

/*            var vecfeatures=vectorLayer.getSource().getFeatures();
            if(vecfeatures.length>0) {
                map.getView().setCenter(ol.extent.getCenter(vecfeatures[0].getGeometry().getExtent()));
            }*/
        }
/*        else {
            if (!$("#map-map").data(mapUrl)) {
                var layer = new ol.layer.Tile({
                    source: new ol.source.TileArcGISRest({
                        url: mapUrl
                    })
                });
                map.addLayer(layer);
                $("#map-map").data(mapUrl, layer);
            } else {
                layer = $("#map-map").data(mapUrl);
                map.removeLayer(layer);
                $("#map-map").removeData(mapUrl);
            }
        }*/
        //设置地图中心点
        /*map.getView().setCenter(ol.proj.transform([114.034428, 22.535805], 'EPSG:4326', 'EPSG:900913'));*/
    }

    function initial() {
        var doc = document;
        var info = statistical;
        var mainPopup = $("#popup").find("div.css-popup");
        var content = info[0].content;
        var dl = mainPopup.find("dl");
        mainPopup.find("p.popup-title").html(info[0].title);
        content.forEach(function (text) {
            $("<dt>", {
                text: text + " :"
            }).appendTo(dl);
            $("<dd>", {
                text: "xxx",
                "data-url": info[0].url,
                "data-title": info[0].title,
                "data-content": info[0].content
            }).appendTo(dl);
        });
        mainPopup.find("p.js-sum").html(info[0].index + "/" + info.length);
    }

    function getNext() {
        var info = statistical;
        var mainPopup = $("#popup").find("div.css-popup");
        var index =  parseInt(mainPopup.find("p.js-sum").html());
        var item = info[index++ % info.length];
        var content = item.content;
        var dl = mainPopup.find("dl");
        mainPopup.find("p.popup-title").html(item.title);
        dl.html("");
        content.forEach(function (text) {
            $("<dt>", {
                text: text + " :"
            }).appendTo(dl);
            $("<dd>", {
                text: "xxx",
                "data-url": item.url,
                "data-title": item.title,
                "data-content": item.content
            }).appendTo(dl);
        });
        mainPopup.find("p.js-sum").html(item.index + "/" + info.length);
    }

    function getPre() {
        var info = statistical;
        var mainPopup = $("#popup").find("div.css-popup");
        var index =  parseInt(mainPopup.find("p.js-sum").html());
        var preIndex = index === 1? info.length - 1 : index - 2;
        var item = info[preIndex];
        var content = item.content;
        var dl = mainPopup.find("dl");
        mainPopup.find("p.popup-title").html(item.title);
        dl.html("");
        content.forEach(function (text) {
            $("<dt>", {
                text: text + " :"
            }).appendTo(dl);
            $("<dd>", {
                text: "xxx",
                "data-url": item.url,
                "data-title": item.title,
                "data-content": item.content
            }).appendTo(dl);
        });
        mainPopup.find("p.js-sum").html(item.index + "/" + info.length);
    }


    function hide(target) {
        $(target).removeClass("click");
        $("#popup").addClass("hidden");
    }

    function show(target) {
        $(target).addClass("click");
        $("#popup").removeClass("hidden");
    }
    return {
        initial: initial,
        toggleLayer: toggleLayer,
        getNext: getNext,
        getPre: getPre,
        hide: hide,
        show: show
    };
});